package com.woniu.service.impl;

import com.woniu.entity.RawMaterial;
import com.woniu.mapper.RawMaterialMapper;
import com.woniu.service.RawMaterialService;
import com.woniu.util.ResponseEnum;
import com.woniu.util.ResponseUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class RawMaterialServiceImpl implements RawMaterialService {
    @Resource
    private RawMaterialMapper mapper;

    @Override
    public ResponseUtil findAll() {
        List<RawMaterial> list = mapper.findAll();
        return !list.isEmpty() ? ResponseUtil.get(ResponseEnum.OK, list)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil findOne() {
        List<RawMaterial> list = mapper.findOne();
        return !list.isEmpty() ? ResponseUtil.get(ResponseEnum.OK, list)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil findTwo() {
        List<RawMaterial> list = mapper.findTwo();
        return !list.isEmpty() ? ResponseUtil.get(ResponseEnum.OK, list)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil findByMaterialId(Integer materialId) {
        RawMaterial obj = mapper.findByMaterialId(materialId);
        return obj != null ? ResponseUtil.get(ResponseEnum.OK, obj)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }
    @Override
    public ResponseUtil add(RawMaterial rawMaterial) {
        rawMaterial.setTotalPrice(
                rawMaterial.getNum()
                        .multiply(rawMaterial.getPrice())   // BigDecimal.multiply
        );
        return mapper.add(rawMaterial) > 0
                ? ResponseUtil.get(ResponseEnum.OK)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }
    @Override
    public ResponseUtil delete(Integer materialId) {
        return mapper.delete(materialId) > 0
                ? ResponseUtil.get(ResponseEnum.OK)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }
    @Override
    public ResponseUtil update(RawMaterial rawMaterial) {
        return mapper.update(rawMaterial) > 0
                ? ResponseUtil.get(ResponseEnum.OK)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil update1(Integer materialId, Integer status) {
        return mapper.update1(materialId,status) > 0
                ? ResponseUtil.get(ResponseEnum.OK)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil findByStatus() {
        List<RawMaterial> list = mapper.findByStatus();
        return !list.isEmpty() ? ResponseUtil.get(ResponseEnum.OK, list)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil findByCondition(RawMaterial rawMaterial) {
        List<RawMaterial> list = mapper.findByCondition(rawMaterial);
        return !list.isEmpty() ? ResponseUtil.get(ResponseEnum.OK, list)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil findByCondition1(RawMaterial rawMaterial) {
        List<RawMaterial> list = mapper.findByCondition1(rawMaterial);
        return !list.isEmpty() ? ResponseUtil.get(ResponseEnum.OK, list)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil findByCondition2(RawMaterial rawMaterial) {
        List<RawMaterial> list = mapper.findByCondition2(rawMaterial);
        return !list.isEmpty() ? ResponseUtil.get(ResponseEnum.OK, list)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }
}
